var viewer = null;
var selectVal = 0;
$(function() {
	vm.loadList();
	$("#select_type").change(function() {
		var type = $("#select_type").val();
		if(type == 1) {
			$("#search_input").attr("placeholder", "请输入商户名");
		} else if(type == 2) {
			$("#search_input").attr("placeholder", "请输入商户号");
		} else if(type == 3) {
			$("#search_input").attr("placeholder", "请输入手机号");
		} else if(type == 4) {
			$("#search_input").attr("placeholder", "请输入身份证号");
		}
	});
	$('input[name=inlineRadioOptions5]').click(function() {
		var selectedId = $(this).val();
		if(selectedId == 1) {
			// 审核通过
			vm.refuse = false;
			vm.blackList = false;
		} else if(selectedId == 2) {
			// 拒绝
			vm.causeType = 1;
			vm.blackList = false;
			vm.refuse = true;
		} else {
			vm.causeType = 2;
			vm.blackList = true;
			vm.refuse = false;
		}
		vm.loadCauseList();
		selectVal = selectedId;
	});
	$("#refuseSelect").change(function() {
		$("#refuseInput").val($(this).val());
	});
	$("#blackListSelect").change(function() {
		$("#blackListInput").val($(this).val());
	});
});

function showImages(index) {
	if(viewer == null) {
		viewer = new Viewer(document.getElementById("images"), {
			url: 'src',
		});
	}
	if(viewer != null) {
		viewer.view(index).show();
	}
}

var vm = new Vue({
	el: "#rrapp",
	data: {
		showList: true,
		showInfo: false,
		blackList: false,
		bankLoad:false,
		isAuth:false,
		causeType:1,
		idCardLoad: false,
		refuse: false,
		causeList:[],
		pageData: {
			limit: 10,
			page: 1
		},
		dataList: [],
		dataInfo: {}
	},
	methods: {
		loadList: function() {
			var dataJson = "{\"page\":\"" + vm.pageData.page + "\",\"limit\":\"" + vm.pageData.limit + "\"}";
			$.ajax({
				type: "POST",
				url: "/pos/audit/list",
				contentType: "application/json;charset=utf-8",
				data: dataJson,
				dataType: "JSON",
				error: function(r) {
					alert("数据加载失败");
					console.log("数据加载失败", r);
				},
				success: function(r) {
					if(r.code == 0) {
						vm.dataList = r.data.list;
					} else {
						alert(r.msg);
					}
				},
				async: true
			});
		},
		loadCauseList:function(){
			$.ajax({
				type:"get",
				url:"/cause/list?type="+vm.causeType+"&page=1&limit=100",
				success:function(r){
					console.log("data",r.data);
					if (r.code == 0){
						vm.causeList = r.data.list;
					}else{
						alert(r.msg);
					}
				},
				error:function(e){
					alert("原因列表加载失败");
				},
				async:true
			});
		},
		showImage: function(index) {
			//			if(viewer != null) {
			//				viewer.view(index).show();
			//			}
			showImages(index);
		},
		query: function() {
			var dataJosn = '{"searchType":"' + $("#select_type").val() + '","search":"' + $("#search_input").val() + '","page":' + vm.pageData.page + ',"limit":' + vm.pageData.limit + '}';
			$.ajax({
				type: "post",
				url: "/pos/audit/search",
				contentType: "application/json;charset=utf-8",
				data: dataJosn,
				dataType: "JSON",
				success: function(r) {
					if(r.code == 0) {
						vm.dataList = r.data.list;
						console.log("data", r);
					} else {
						alert(r.msg);
					}
				},
				error: function(e) {
					alert("加载数据出错");
					console.log("加载出错", e);
				},
				async: true
			});
		},
		info: function(id) {
			vm.showInfo = true;
			vm.showList = false;
			$.ajax({
				type: "get",
				url: "/pos/audit/info?id=" + id,
				dataType: "json",
				success: function(r) {
					if(r.code == 0) {
						vm.dataInfo = r.data;
						if(vm.dataInfo.frist) {
							vm.dataInfo.frist = "是";
						} else {
							vm.dataInfo.frist = "否";
						}
						if (r.data.realAuth == 1){
							vm.isAuth = true;
						}else {
							vm.isAuth = false;
						}
						// 是否开通T1
						if(vm.dataInfo.openT1) {
							$("#openT11").prop('checked', true);
							$("#openT12").prop('checked', false);
						} else {
							$("#openT12").prop('checked', true);
							$("#openT11").prop('checked', false);
						}

						console.log("T0", vm.dataInfo.openT0);
						// 是否开通T0
						if(vm.dataInfo.openT0) {
							$("#openT02").prop('checked', false);
							$("#openT01").prop('checked', true);
						} else {
							$("#openT02").prop('checked', true);
							$("#openT01").prop('checked', false);
						}

						console.log("settlement", vm.dataInfo.settlement);
						// 是否开通结算
						if(vm.dataInfo.settlement) {
							$("#settlement2").prop('checked', false);
							$("#settlement1").prop('checked', true);
						} else {
							$("#settlement2").prop('checked', true);
							$("#settlement1").prop('checked', false);
						}

						console.log("autoSettlement", vm.dataInfo.autoSettlement);
						// 是否开通自动结算
						if(vm.dataInfo.autoSettlement) {
							$("#autoSettlement2").prop('checked', false);
							$("#autoSettlement1").prop('checked', true);
						} else {
							$("#autoSettlement2").prop('checked', true);
							$("#autoSettlement1").prop('checked', false);
						}
					} else {
						alert(r.msg);
					}
				},
				error: function(e) {
					alert("数据获取失败");
				},
				async: true
			});
		},
		examine: function() {
			var cause = null;
			var urlStr = "";
			if(selectVal == 0) {
				alert("请选择审核意见");
				return;
			} else if(selectVal == 1) {
				// 通过
				urlStr = "/pos/audit/pass";
			} else if(selectVal == 2) {
				// 拒绝
				cause = $("#refuseInput").val();
				urlStr = "/pos/audit/refuse";
			} else if (selectVal == 3){
				// 拉黑
				urlStr = "/pos/audit/blackList";
				cause = $("#blackListInput").val();
			}
			if(selectVal > 1 && cause == null) {
				alert("请填写原因");
				return;
			} else {
				urlStr += "?id=" + vm.dataInfo.merId + "&remarks=" + cause;
				$.ajax({
					type: "get",
					url: urlStr,
					success: function(r) {
						if(r.code == 0) {
							alert("处理成功");
							vm.reload();
						} else {
							alert(r.msg);
						}
					},
					error: function(e) {
						alert("数据提交失败");
					},
					async: true
				});
			}
		},
		bankCheck: function(index) {
			vm.bankLoad = true;
			var bank = vm.dataInfo.banks[index];
			console.log("bank",bank);
			var name = bank.accName;
			var phone = vm.dataInfo.phone;
			var idCard = vm.dataInfo.idCard;
			var bankNumber = bank.accNum;
			var loadIcon = document.getElementById("bankIcon");
			loadIcon.style.color = "#000000";
			loadIcon.innerHTML = "&#xe63d;";
			if(name == null || phone == null || idCard == null || bankNumber == null) {
				alert("参数不齐全");
				return;
			}
			var dataStr = '{"realName":"'+name+'","idCard":"'+idCard+'","mobile":"'+phone+'","bankCard":"'+bankNumber+'","bankCardReason":"实名审核","type":4}';
			$.ajax({
				type: "post",
				url: "/pos/auth/bank",
				dataType: "json",
				data:dataStr,
				contentType:"application/json;charset=UTF-8",
				success: function(r) {
					if(r.code == 0) {
						if (r.data.record[0].resCode == "00"){
							loadIcon.style.color = "green";
							loadIcon.innerHTML = "&#xe605;";
						}else{
							loadIcon.style.color = "red";
							loadIcon.innerHTML = "&#x1006;";
							alert(r.data.record[0].resDesc);
						}
					} else {
						loadIcon.style.color = "red";
						loadIcon.innerHTML = "&#x1006;";
						alert(r.msg);
					}
				},
				error: function(e) {
					loadIcon.style.color = "red";
					loadIcon.innerHTML = "&#x1006;";
					alert("请求发送失败");
				},
				async: true
			});
		},
		idCardCheck: function() {
			vm.idCardLoad = true;
			var loadIcon = document.getElementById("idCardIcon");
			var idCard = vm.dataInfo.idCard;
			var name = vm.dataInfo.merName;
			if(idCard == null || name == null) {
				alert("资料不齐全");
				return;
			}
			loadIcon.innerHTML = "&#xe63d;";
			loadIcon.style.color = "#000000";
			var dataStr = '{"idCard":"' + idCard + '","realName":"' + name + '","typed":0,"idCardReason":"实名审核"}'
			$.ajax({
				type: "post",
				url: "/pos/auth/idCard",
				dataType: 'json',
				contentType: "application/json;charset=UTF-8",
				data: dataStr,
				success: function(r) {
					if(r.code == 0) {
						console.log("实名：", r.data);
						if(r.data.record[0].resCode == "00") {
							loadIcon.style.color = "green";
							loadIcon.innerHTML = "&#xe605;";
						} else {
							loadIcon.style.color = "red";
							loadIcon.innerHTML = "&#x1006;";
							alert(r.data.record[0].resDesc);
						}

					} else {
						loadIcon.style.color = "red";
						loadIcon.innerHTML = "&#x1006;";
						alert(r.msg);
					}
				},
				error: function(e) {
					loadIcon.style.color = "red";
					loadIcon.innerHTML = "&#x1006;";
					alert("数据请求失败");
				},
				async: true
			});
		},
		close: function() {
			vm.reload();
		},
		reload: function() {
			vm.showList = true;
			vm.showInfo = false;
			vm.bankLoad = false;
			vm.idCardLoad = false;
			document.getElementById("idCardIcon").innerHTML = "";
			document.getElementById("idCardIcon").style.color = "#000000";
			document.getElementById("bankIcon").innerHTML = "";
			document.getElementById("bankIcon").style.color = "#000000";
			this.loadList();
		}
	}
});